package com.atues.utils;


import com.atues.domain.vo.HotArticleVo;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.stream.Collectors;

public class BeanCopyUtils {
    private BeanCopyUtils(){

    }

    public static<T> T copyBean(Object source,Class<T> clazz) {
        //创建目标对象
        T o;
        try {
            o = clazz.newInstance();
            //实现属性拷贝
            BeanUtils.copyProperties(source, o);


        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }

        return o;

    }

    public static<O,T>  List<T> copyBeanList(List<O> list,Class<T> clazz){
        return  list.stream()
                .map(o -> copyBean(o, clazz))
                .collect(Collectors.toList());
    }

}
